<?php
class Dashboard_Model_App
{
    /**
     * Checks if a resource exists for this App
     * 
     * @param array $module
     * 
     * @return boolean
     */
    private function checkAccess($module)
    {
        try {
            $object = Zend_Session::namespaceGet('Zend_Object');
            $resource = $module . '_app';
            // Admin see all apps
            if($object['acl']->has('all')) {
                return TRUE;
            }
            return $object['acl']->has($resource);
            
        } catch (Exception $e) {

            return FALSE;
        }
    }
    /**
     * Search all Apps
     * 
     * @return boolean 
     */
    public function getApps()
    {
        try {
            $apps = array();
            $modules = array();

            $front = Zend_Controller_Front::getInstance();
            $modules = $front->getControllerDirectory();

            foreach ($modules as $module => $path) {

                //Checks if the config file exists
                $appIni =
                    realpath(
                        $path . DIRECTORY_SEPARATOR . ".." .
                        DIRECTORY_SEPARATOR . "configs" .
                        DIRECTORY_SEPARATOR . "app.ini");
                
                if ($appIni != FALSE && $this->checkAccess($module) === TRUE) {
                    // App data are stored into a new array
                    $apps[$module]['path'] = $path;
                    // Load header data
                    $appData = new Zend_Config_Ini($appIni, 'app');
                    // Merging all datas in new array
                    $apps[$module] = array_merge($apps[$module], $appData->toArray());
                }
            }
            return $apps;
        } catch (Exception $e) {

            return FALSE;
        }
    }
    /**
     * Reads all favorite apps
     * 
     * @throws Exception Favorites INI file not found!
     * 
     * @return boolean
     */
    public function getFavorites()
    {
        try {
            $apps = array();

            $front = Zend_Controller_Front::getInstance();
            $file = realpath(
                        APPLICATION_PATH . DIRECTORY_SEPARATOR .
                        'modules' . DIRECTORY_SEPARATOR .
                        'dashboard' . DIRECTORY_SEPARATOR .
                        'configs' . DIRECTORY_SEPARATOR .
                        'favorites.ini');
            if ($file === FALSE) {
                throw new Exception('Favorites INI file not found!');
            }
            $configFavorites = new Zend_Config_Ini($file, 'favorites');
            $favorites = $configFavorites->app->toArray();

            foreach ($favorites as $favorite) {
                
                $appIni = realpath(
                    APPLICATION_PATH . DIRECTORY_SEPARATOR .
                    'modules' . DIRECTORY_SEPARATOR .
                    $favorite . DIRECTORY_SEPARATOR .
                    'configs' . DIRECTORY_SEPARATOR .
                    'app.ini');
                if ($appIni ==! FALSE && $this->checkAccess($favorite) != FALSE) {
                    $appData = new Zend_Config_Ini($appIni, 'app');
                    $apps[$favorite] = $appData->toArray();
                }
            }
            return $apps;
        } catch (Exception $e) {

            return FALSE;
        }
    }
}